<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>
<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import { debounce } from "@/utils";

const animationDuration = 6000;

export default {
  props: {
    className: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "400px"
    },
    barData: {
      type: Object
    },
    loading: {
      default: true
    }
  },
  data() {
    return {
      chart: null,
      options: "",
      tableData: []
    };
  },
  watch: {
    barData: {
      handler(newValue, oldValue) {
        console.log("oldValue", oldValue);
        console.log("newvalue", newValue);
        this.barData = newValue; //把新值赋值给我们的属性数据
        this.initChart(); //刷新echarts图表
      },
      deep: true
    }
  },
  mounted() {
    this.initChart();
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize();
      }
    }, 100);
    window.addEventListener("resize", this.__resizeHandler);
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    window.removeEventListener("resize", this.__resizeHandler);
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");

      let option = {
        color: ["#a4d8c2", "#ffb980", "#5ab1ef", "#2ec7c9", "#5ab1ef"],
        legend: {},
        title: {
          x: "4%",
          textStyle: {
            color: "#fff",
            fontSize: "22"
          },
          subtextStyle: {
            color: "#90979c",
            fontSize: "16"
          }
        },
        toolbox: {
          //可视化的工具箱
          show: true,
          feature: {
            dataView: {
              //数据视图
              show: true,
              readOnly: true,
              optionToContent: function(opt) {
                console.log("opt", opt);
                // const axisData = opt.xAxis[0].data;
                // const series = opt.series;
                const data = opt.dataset[0].source;
                let table =
                  '<table class="bordered" style="width:100%;text-align:center"><tbody><tr>' +
                  "<td>时间</td>" +
                  "<td>" +
                  data[0][1] +
                  "</td>" +
                  "<td>" +
                  data[0][2] +
                  "</td>" +
                  "</tr>";
                for (var i = 1, l = data.length; i < l; i++) {
                  table +=
                    "<tr>" +
                    "<td>" +
                    data[i][0] +
                    "</td>" +
                    "<td>" +
                    data[i][1] +
                    "</td>" +
                    "<td>" +
                    data[i][2] +
                    "</td>" +
                    "</tr>";
                }
                table += "</tbody></table>";
                return table;
              }
            },
            restore: {
              //重置
              show: true
            },
            // dataZoom: {
            //   //数据缩放视图
            //   show: true
            // },
            // saveAsImage: {
            //   //保存图片
            //   show: true
            // },
            magicType: {
              //动态类型切换
              type: ["line", "bar"]
              // type: ["line", "bar", "stack", "tiled"]
            }
          }
        },

        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
            textStyle: {
              color: "#fff"
            }
          }
        },
        grid: {
          borderWidth: 0,
          top: 80,
          textStyle: {
            color: "#fff"
          }
        },
        legend: {
          x: "4%",
          top: "8%",
          textStyle: {
            color: "#90979c"
          }
        },
        calculable: true,
        xAxis: [
          {
            type: "category",
            axisLine: {
              lineStyle: {
                color: "#90979c"
              }
            },
            splitLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            splitArea: {
              show: false
            },
            axisLabel: {
              interval: 0
            }
          }
        ],
        yAxis: [
          {
            type: "value",
            splitLine: {
              show: false
            },
            axisLine: {
              lineStyle: {
                color: "#90979c"
              }
            },
            axisTick: {
              show: false
            },
            axisLabel: {
              interval: 0
            },
            splitArea: {
              show: false
            }
          }
        ],
        dataZoom: [
          {
            show: true,
            height: 30,
            xAxisIndex: [0],
            bottom: 30,
            end: 100,
            handleIcon:
              "path://M306.1,413c0,2.2-1.8,4-4,4h-59.8c-2.2,0-4-1.8-4-4V200.8c0-2.2,1.8-4,4-4h59.8c2.2,0,4,1.8,4,4V413z",
            handleSize: "110%",
            handleStyle: {
              color: "#d3dee5"
            },
            textStyle: {
              color: "#fff"
            },
            borderColor: "#90979c"
          },
          {
            type: "inside",
            show: true,
            height: 15,
            start: 1,
            end: 35
          }
        ],
        series: [
          {
            type: "bar",
            color:"#67e0e3",
            // itemStyle: {
            //   normal: {
            //     // color: "rgba(255,144,128,1)",
            //     color: new echarts.graphic.LinearGradient(
            //       1,
            //       0,
            //       0,
            //       1,
            //       [
            //         {
            //           offset: 0,
            //           color: "#412fe3" // 0% 处的颜色 紫
            //         },
            //         {
            //           offset: 1,
            //           color: "#d8d5f9" // 100% 处的颜色
            //         }
            //       ],
            //       false
            //     ),
            //     label: {
            //       show: true,
            //       textStyle: {
            //         color: "#303133"
            //       },
            //       position: "top",
            //       formatter: function(p) {
            //         return p.value > 0 ? p.value : "";
            //       }
            //     }
            //   }
            // },
            lineStyle: {
              width: 4,
              shadowColor: "#412fe3",
              shadowOffsetY: 2,
              shadowBlur: 2,
              opacity: 0.5
            }
          },
          {
            type: "bar",
            color:"#37a2da",
            // itemStyle: {
            //   normal: {
            //     color: new echarts.graphic.LinearGradient(
            //       1,
            //       0,
            //       0,
            //       1,
            //       [
            //         {
            //           offset: 0,
            //           color: "#0c6beb" // 0% 处的颜色 蓝
            //         },
            //         {
            //           offset: 1,
            //           color: "#f0f6fe" // 100% 处的颜色
            //         }
            //       ],
            //       false
            //     ),
            //     barBorderRadius: 0,
            //     borderWidth: 1,
            //     label: {
            //       show: true,
            //       position: "top",
            //       formatter: function(p) {
            //         return p.value > 0 ? p.value : "";
            //       }
            //     }
            //   }
            // },
            lineStyle: {
              width: 4,
              shadowColor: "#f0f6fe",
              shadowOffsetY: 2,
              shadowBlur: 2,
              opacity: 0.5
            }
          }
        ]
      };
      this.chart.setOption(option);
      this.chart.setOption(this.barData);
    }
  }
};
</script>
<style>
table {
  /* cellspacing: 0; */
  *border-collapse: collapse; /* IE7 and lower */
  border-spacing: 0;
  width: 100%;
}
.bordered tr:hover {
  background: #fbf8e9;
  -o-transition: all 0.1s ease-in-out;
  -webkit-transition: all 0.1s ease-in-out;
  -moz-transition: all 0.1s ease-in-out;
  -ms-transition: all 0.1s ease-in-out;
  transition: all 0.1s ease-in-out;
}

.bordered th {
  padding: 7px;
  text-align: center;
  /* cellspacing: 0; */
}

.bordered td {
  padding: 7px;
  text-align: center;
  /* cellspacing: 0; */
}

.bordered th {
  background-image: -webkit-gradient(
    linear,
    left top,
    left bottom,
    from(#ebf3fc),
    to(#dce9f9)
  );
  background-image: -webkit-linear-gradient(top, #ebf3fc, #dce9f9);
  background-image: -moz-linear-gradient(top, #ebf3fc, #dce9f9);
  background-image: -ms-linear-gradient(top, #ebf3fc, #dce9f9);
  background-image: -o-linear-gradient(top, #ebf3fc, #dce9f9);
  background-image: linear-gradient(top, #ebf3fc, #dce9f9);
}
.bordered td:first-child,
.bordered th:first-child {
  border-left: none;
}

.bordered tr:nth-of-type(2n) {
  background: #ffffff;
  cursor: pointer;
}
.bordered tr:nth-of-type(2n + 1) {
  background: #f7fafc;
  cursor: pointer;
}

.bordered tbody tr:hover {
  background: #fbf8e9;
  -o-transition: all 0.1s ease-in-out;
  -webkit-transition: all 0.1s ease-in-out;
  -moz-transition: all 0.1s ease-in-out;
  -ms-transition: all 0.1s ease-in-out;
  transition: all 0.1s ease-in-out;
}
</style>
